package com.sunday.shoufen.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.shoufen.R;
import com.sunday.shoufen.base.BaseApp;
import com.sunday.shoufen.entity.Member;
import com.sunday.shoufen.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.shoufen.http.ApiClient.WEB_URL;

/**
 * Created by 刘涛 on 2017/10/30.
 */

public class RegisterFinishActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_pwd)
    ClearEditText editPwd;
    @Bind(R.id.edit_confirm_pwd)
    ClearEditText editConfirmPwd;
    @Bind(R.id.btn_register)
    TextView btnRegister;
    @Bind(R.id.protocol_check)
    ImageButton protocolCheck;
    @Bind(R.id.txt_protocol)
    TextView txtProtocol;
    @Bind(R.id.ll_regis)
    LinearLayout llRgis;

    private String mobile, code;
    private int type;//0:注册 1：修改密码 2：忘记密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_finish);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mobile = getIntent().getStringExtra("mobile");
        code = getIntent().getStringExtra("code");
        type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case 0:
                titleView.setText("注册");
                break;
            case 1:
                titleView.setText("修改密码");
                llRgis.setVisibility(View.GONE);
                btnRegister.setText("确认");
                break;
            case 2:
                titleView.setText("忘记密码");
                llRgis.setVisibility(View.GONE);
                btnRegister.setText("确认");
                break;
        }
    }

    private boolean isAgree = false;
    private String pwd, pwd1;

    @OnClick({R.id.btn_register, R.id.txt_protocol, R.id.protocol_check})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                pwd = editPwd.getText().toString().trim();
                pwd1 = editConfirmPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd1)) {
                    ToastUtils.showToast(mContext, "请输入密码");
                    return;
                }
                if (!pwd.equals(pwd1)) {
                    ToastUtils.showToast(mContext, "两次密码输入不一致");
                    return;
                }
                switch (type) {
                    case 0:
                        titleView.setText("注册");
                        if (!isAgree) {
                            ToastUtils.showToast(mContext, "请阅读并同意用户协议");
                            return;
                        } else {
                            register();
                        }
                        break;
                    case 1:
                        titleView.setText("修改密码");
                        llRgis.setVisibility(View.GONE);
                        updatePwd();
                        break;
                    case 2:
                        titleView.setText("忘记密码");
                        llRgis.setVisibility(View.GONE);
                        updatePwd();
                        break;
                }

                break;
            case R.id.txt_protocol:
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", WEB_URL + "zcxy");
                startActivity(intent);
                break;
            case R.id.protocol_check:
                if (!isAgree) {
                    protocolCheck.setSelected(true);
                } else {
                    protocolCheck.setSelected(false);
                }
                isAgree = !isAgree;
                break;
        }
    }

    private void register() {
        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().register(mobile, code, pwd);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Member> resultDO = response.body();
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "注册成功！");
                    SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                    Member member = resultDO.getResult();
                    BaseApp.getInstance().setMember(member);
                    intent = new Intent(mContext, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private void updatePwd() {
        Call<ResultDO> call = ApiClient.getApiAdapter().findPwd(mobile, code, pwd);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Member> resultDO = response.body();
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "设置成功");
                    switch (type) {
                        case 1:
                            finish();
                            break;
                        case 2:
                            finish();
                            break;
                    }
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }
}
